import React, { useEffect, useState } from 'react';
import { connect } from 'umi';
import { Result } from 'antd';

const Office = (props) => {
  const { dispatch, location } = props;
  const [message, setMessage] = useState();

  useEffect(() => {
    const load = async () => {
      dispatch({
        type: 'resources/view',
        payload: { rid: location.query.rid },
        callback: (res) => {
          const { data } = res;
          if (!res.success) {
            setMessage(res.msg);
          } else {
            window.open(`./#office/view?rid=${data.rid}`, '_self');
          }
        },
      });
    };
    load();
  }, []);

  return <Result key="result" status="info" title={message} />;
};

export default connect(({ resources, loading }) => ({
  resources,
  loading: loading.effects['resources/view'],
}))(Office);
